package com.cloud.servlet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.core.BaseServlet;
import com.cloud.dao.CrcBean;
import com.cloud.service.CrcService;
import com.cloud.service.FileService;
import com.cloud.util.Code;
import com.cloud.util.CommUtil;

/**
 * 1Cloud1
 * @author Administrator
 * @since 2016-1-17
 * @version 1.0 
 */
@Controller
@RequestMapping("/crc")
public class CrcServlet extends BaseServlet {
	private static final Logger logger = LoggerFactory.getLogger(CrcServlet.class);
	@Autowired
	private CrcService CrcService;
	@Autowired
	private FileService FileService;
	
	/**
	 * 查询CRC信息
	 * @param request
	 * @param patient
	 * @return
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.GET})
	public @ResponseBody Object list(HttpServletRequest request, CrcBean crc) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(crc == null || CommUtil.isEmpty(crc.getUserId()) || (CommUtil.isEmpty(crc.getId()) && CommUtil.isEmpty(crc.getUserName()))){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		logger.debug(String.format("开始查询CRC[%s][%s]信息", crc.getId(), crc.getUserName()));
		List<CrcBean> retCrcList = new ArrayList<CrcBean>();
		retCrcList = CrcService.getCrcList(crc);
		List<Map<String, String>> _mapList = new ArrayList<Map<String, String>>();
		for(CrcBean _crc : retCrcList){
			Map<String, String> _map = new HashMap<String, String>();
			_map.put("id", _crc.getId());
			_map.put("userName", _crc.getUserName());
			_map.put("name", _crc.getFullname());
			_map.put("crcHead", FileService.getVisitUrl(_crc.getCrc()));
			_map.put("comName", _crc.getCom_id());
			_map.put("desc", _crc.getIntroduction());
			_map.put("status", _crc.getStatus());
			_mapList.add(_map);
		}
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		map.put("data", _mapList);
		return map;
	}
	
	/**
	 * 修改CRC信息
	 * @param request
	 * @param patient
	 * @return
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST})
	public @ResponseBody Object pupload(HttpServletRequest request, @RequestBody CrcBean crc) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(crc == null || (CommUtil.isEmpty(crc.getId()))){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		logger.debug(String.format("开始修改CRC[%s]信息", crc.getId()));
		try {
			CrcService.updateCrc(crc);
		} catch (Exception e) {
			logger.error("修改CRC信息失败.", e);
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
}
